package com.andreas.persistence;

import com.andreas.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 描述：消费者2
 */
public class Recer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection connection = ConnectionUtils.getConnection();
        // 获得信道
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare("test_exchange_topic_queue_2", true, false, false, null);
        // 将队列绑定在topic路由上
        channel.queueBind("test_exchange_topic_queue_2", "test_exchange_topic",
                "product.#");
        // 定义消费
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String s = new String(body);
                System.out.println("【消费者2】 = " + s);
            }
        };
        // 消费者监听队列的情况
        // 第二个参数是否为持久化，true表示的是持久化的操作。channel持久化的操作，要将对应的位置持久化修改。
        channel.basicConsume("test_exchange_topic_queue_2", true, defaultConsumer);
    }
}
